package com.youbug.mall.product_pms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.youbug.common.vo.QueryVo;
import com.youbug.common.vo.R;
import com.youbug.mall.product_pms.entity.PmsAttrGroup;
import com.youbug.mall.product_pms.entity.PmsAttr;
import com.youbug.mall.product_pms.entity.PmsAttrAttrgroupRelation;
import com.youbug.mall.product_pms.service.IAttrGroupService;
import com.youbug.mall.product_pms.service.ICategoryService;
import com.youbug.mall.product_pms.service.IPmsAttrAttrgroupRelationService;
import com.youbug.mall.product_pms.vo.AttrGroupRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 属性分组 前端控制器
 * </p>
 *
 * @author youbug
 * @since 2023-07-08
 */
@RestController
@RequestMapping("/pmsAttrGroup")
public class AttrGroupController {

    @Autowired
    private IAttrGroupService attrGroupService;

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private IPmsAttrAttrgroupRelationService attrAttrgroupRelationService;

    @PostMapping("/getAttrGroupsByCateId")
    public R<IPage<PmsAttrGroup>> getAttrGroupsByCateId(@RequestBody QueryVo<PmsAttrGroup> queryVo){

        IPage<PmsAttrGroup> attrGroupPage = attrGroupService.getAttrGroupPage(queryVo);

        return R.ok(attrGroupPage);
    }

    @PostMapping("/getRelationAttr")
    public R<List<PmsAttr>> getRelationAttr(@RequestBody QueryVo<Long> queryVo){

        List<PmsAttr> attrs = attrGroupService.getRelationAttr(queryVo);

        return R.ok(attrs);
    }

    @PostMapping("/deleteRelation")
    public R<Boolean> deleteRelation(@RequestBody List<AttrGroupRelationVo> relationVos){

        Boolean remove = attrGroupService.deleteRelation(relationVos);

        return R.ok(remove);
    }

    @PostMapping("/addRelations")
    public R<Boolean> addRelations(@RequestBody List<AttrGroupRelationVo> relationVos){

        Boolean add = attrGroupService.addRelations(relationVos);

        return R.ok(add);
    }

    @PostMapping("/getNoAttrRelation")
    public R<IPage<PmsAttr>> getNoAttRelation(@RequestBody QueryVo<PmsAttr> queryVo){

        IPage<PmsAttr> attrs = attrGroupService.getNoAttRelation(queryVo);

        return R.ok(attrs);
    }

    @GetMapping("/getAttrGroupById/{id}")
    public R<PmsAttrGroup> getAttrGroupById(@PathVariable Long id){

        PmsAttrGroup attrGroup = attrGroupService.getById(id);

        Long catalogId = attrGroup.getCatalogId();

        attrGroup.setCatalogPath(categoryService.findCatalogPath(catalogId));

        return R.ok(attrGroup);
    }

    @GetMapping("/getAttrGroupWithAttrByCatalogId/{catalogId}")
    public R<List<PmsAttrGroup>> getAttrGroupWithAttrByCatalogId(@PathVariable Long catalogId){

        List<PmsAttrGroup> attrGroups = attrGroupService.getAttrGroupWithAttrByCatalogId(catalogId);

        return R.ok(attrGroups);
    }

    @GetMapping("/delete/{id}")
    @Transactional(transactionManager = "transactionManagerPms", rollbackFor = Exception.class)
    public R<Boolean> delete(@PathVariable Long id){

        boolean remove = attrAttrgroupRelationService.remove(new QueryWrapper<PmsAttrAttrgroupRelation>().eq("attr_group_id", id));

        boolean remove1 = attrGroupService.removeById(id);

        return R.ok(remove&&remove1);
    }

    @PostMapping("/add")
    public R<Boolean> add(@RequestBody PmsAttrGroup attrGroup){

        boolean save = attrGroupService.save(attrGroup);

        return R.ok(save);
    }

    @PostMapping("/update")
    public R<Boolean> update(@RequestBody PmsAttrGroup attrGroup){

        boolean update = attrGroupService.updateById(attrGroup);

        return R.ok(update);
    }
}

